package com.neusoft.chatserver.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chatserver.dto.MessageListDTO;
import com.neusoft.chatserver.dto.MessageQuery;
import com.neusoft.chatserver.dto.RespBean;
import com.neusoft.chatserver.entity.Message;
import com.neusoft.chatserver.entity.Users;
import com.neusoft.chatserver.service.IMessageService;
import com.neusoft.chatserver.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liuxp
 * @since 2021-07-14
 */

@CrossOrigin
@RestController
@RequestMapping("/chat")
public class MessageController {
    @Autowired
    IMessageService iMessageService;

    @Autowired
    IUsersService iUsersService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/getTodayMessage")
    RespBean getTodayMessage(@RequestBody MessageQuery messageQuery) {

        List<Message> todayMessage = iMessageService.getTodayMessage(messageQuery);
        return RespBean.ok(200, "get today message successful", todayMessage);
    }

    @GetMapping("/getTodayMessageList")
    RespBean getTodayMessageList(int id) {
        String s = (String) redisTemplate.opsForHash().get("MessageList", String.valueOf(id));
        if (s == null) {
            s = "[]";
        }
        List<Integer> list = (List<Integer>) JSONObject.parse(s);
        List<Integer> userOnlineStatus = iUsersService.getUserOnlineStatus(list);

        List<MessageListDTO> returnList = new ArrayList<>();
        for (Integer integer : list) {
            Users users = iUsersService.getById(integer);

            List<Message> todayMessage = iMessageService.getTodayMessage(new MessageQuery(id, integer, null));
            Message message = new Message();
            if (todayMessage.size() != 0) {
                message = todayMessage.get(todayMessage.size() - 1);
            }
            int status = userOnlineStatus.get(list.indexOf(integer));
            returnList.add(new MessageListDTO(users, todayMessage, status, message));
        }

        return RespBean.ok(200, "get list", returnList);
    }

    @PostMapping("/addTodayMessageList")
    RespBean addTodayMessageList(@RequestBody JSONObject jsonObject) {
        int fromID = (int) jsonObject.get("fromID");
        int toID = (int) jsonObject.get("toID");
        HashOperations hashOperations = redisTemplate.opsForHash();
        String s = (String) hashOperations.get("MessageList", String.valueOf(fromID));
        List<Integer> list = (List<Integer>) JSONObject.parse(s);
        if (list == null) {
            list = new ArrayList<>();
        }
        if (!list.contains(toID))
            list.add(toID);
        hashOperations.put("MessageList", String.valueOf(fromID), JSONObject.toJSONString(list));
        return RespBean.ok(200, "get list", list);
    }


    @PostMapping("/setMessageRead")
    RespBean setMessageRead(@RequestBody List<Integer> messageList) {
        for (Integer integer : messageList) {
            iMessageService.update(null, Wrappers.<Message>lambdaUpdate().eq(Message::getMsgId, integer).set(Message::getMsgRead, 1));
        }

        return RespBean.ok(200, "set success", messageList);
    }

}

